const http = require('http');

// 登录测试数据 - 使用数据库中实际存在的用户凭据
const postData = JSON.stringify({
  username: 'zs',
  password: '123123'
});

const options = {
  hostname: 'localhost',
  port: 3000,
  path: '/api/users/login',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(postData)
  }
};

console.log('发送登录请求...');
console.log('请求数据:', postData);

const req = http.request(options, (res) => {
  console.log('收到响应，状态码:', res.statusCode);
  console.log('响应头:', JSON.stringify(res.headers, null, 2));
  
  let data = '';

  res.on('data', (chunk) => {
    data += chunk;
  });

  res.on('end', () => {
    console.log('响应数据:', data);
    try {
      const parsedData = JSON.parse(data);
      console.log('解析后的响应数据:', JSON.stringify(parsedData, null, 2));
    } catch (e) {
      console.log('无法解析JSON响应:', e.message);
    }
  });
});

req.on('error', (e) => {
  console.error('请求错误:', e);
  console.error('错误详情:', e.stack);
});

// 发送请求体
req.write(postData);
req.end();